package com.maochang.service.impl;

import com.maochang.mapper.MenuMapper;
import com.maochang.pojo.PsFunction;
import com.maochang.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("MenuServiceImpl")
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper mapper;

    @Resource
    private MenuMapper menuMapper;

    //查询权限
    @Override
    public List<String> seAchPermCode(Integer roled) {
       List<String> aa=  mapper.seAchPermCode(roled);
        return aa;
    }

    @Override
    public List<PsFunction> seCaiDan() {
        //菜单1
        List<PsFunction> listzuiHoua= new ArrayList<>();
        //shiro subject   对象
        Subject subject= SecurityUtils.getSubject();
        //集合1赋值
        List<PsFunction> list1 =menuMapper.searchByParentId(0);
        //一级菜单权限循环鉴定
        for (PsFunction psFunction01:list1) {
            //判断当前 登录人有没有这个权限
            //System.out.println(subject.isPermitted(psFunction01.getId()));
            if(subject.isPermitted(psFunction01.getId())==true)
            {
                ////查询当前 一级列表下的所有二级列表
                List<PsFunction> list2 = menuMapper.searchByParentId(psFunction01.getIdb());
                ////当前用户 具不具备二子菜单权限    在这里我先不考虑
                //System.out.println("你是个啥啊");
                List<PsFunction> L2=list2.stream().filter(psFunction -> subject.isPermitted(psFunction.getId())).collect(Collectors.toList());
                psFunction01.setChildren(list2.isEmpty()?null:L2);
                listzuiHoua.add(psFunction01);
            }
        }
        //System.out.println(i+"i 的值是？？？？");
        ////声明一个map集合
        //Map<PsFunction,List<PsFunction>> map=new HashMap<>();
        ////shiro 对象

        ////所有的一级栏目
        //List<PsFunction> PsFunctionList=menuMapper.searchByParentId(0);
        ////循环一级栏目
        //for (PsFunction psFunction01:PsFunctionList){
        //    //判断当前 登录人有没有这个权限
        //   if(subject.isPermitted(psFunction01.getId()));{
        //       //所有的二级列表
        //        List<PsFunction> List2=menuMapper.searchByParentId(psFunction01.getIdb());
        //       //如果是true 就是   放进去
        //       // List<PsFunction> L2=List2.stream().filter(psFunction -> subject.isPermitted(psFunction.getId())).collect(Collectors.toList());
        //       //再次进行过滤
        //        //对象      //list集合
        //        map.put(psFunction01,List2);
        //    }
        //}
        //return map;
        //System.out.println(listzuiHoua+"筛选数据");
        return  listzuiHoua;
    }
    //查询菜单

}
